//
// Included Files
//
#include "F2837xD_device.h"
#include "driverlib.h"
#include "device.h"
#include "board.h"
#include "utility.h"
#include <string.h>
#include <stdint.h>

#include "Uart.h"
#include "Key.h"
#include "bspADC.h"
#include "IQmathLib.h"
#include "drv8305.h"
#include "bspIpc.h"
#include "foc_declare.h"


uint32_t rxCnt = 0;
uint16_t rDataA[11];
uint16_t rDataPointA;


extern MOTOR_Vars_t motorVars;


__interrupt void INT_mySCI0_RX_ISR(void);
__interrupt void INT_mySCI0_TX_ISR(void);
void LED_init(void);

IPC_MessageQueue_t messageQueue;
IPC_Message_t      TxMsg;

//
// Main
//
int main(void)
{
    Device_init();
    Device_initGPIO();

#ifdef _STANDALONE
#ifdef _FLASH
    // Send boot command to allow the CPU2 application to begin execution
    Device_bootCPU2(C1C2_BROM_BOOTMODE_BOOT_FROM_FLASH);
#else

    Device_bootCPU2(C1C2_BROM_BOOTMODE_BOOT_FROM_RAM);
#endif // _FLASH
#endif // _STANDALONE

    Interrupt_initModule();
    Interrupt_initVectorTable();

    // Disable sync(Freeze clock to PWM as well)
    SysCtl_disablePeripheral(SYSCTL_PERIPH_CLK_TBCLKSYNC);
    Board_init();
    LED_init();

    SysCtl_selectCPUForPeripheral(SYSCTL_CPUSEL5_SCI, 1, SYSCTL_CPUSEL_CPU2);

    IPC_clearFlagLtoR(IPC_CPU1_L_CPU2_R, IPC_FLAG_ALL);
    IPC_initMessageQueue(IPC_CPU1_L_CPU2_R, &messageQueue, IPC_INT1, IPC_INT1);
    IPC_sync(IPC_CPU1_L_CPU2_R, IPC_FLAG31);

    // Enable sync and clock to PWM
    SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_TBCLKSYNC);
    EPWM_disableCounterCompareShadowLoadMode(myEPWM0_BASE, EPWM_COUNTER_COMPARE_A);
    EPWM_disableCounterCompareShadowLoadMode(myEPWM1_BASE, EPWM_COUNTER_COMPARE_A);
    EPWM_disableCounterCompareShadowLoadMode(myEPWM2_BASE, EPWM_COUNTER_COMPARE_A);
//    EPWM_setCounterCompareShadowLoadMode(myEPWM0_BASE, EPWM_COUNTER_COMPARE_A, EPWM_COMP_LOAD_ON_CNTR_ZERO_PERIOD);
//    EPWM_setCounterCompareShadowLoadMode(myEPWM1_BASE, EPWM_COUNTER_COMPARE_A, EPWM_COMP_LOAD_ON_CNTR_ZERO_PERIOD);
//    EPWM_setCounterCompareShadowLoadMode(myEPWM2_BASE, EPWM_COUNTER_COMPARE_A, EPWM_COMP_LOAD_ON_CNTR_ZERO_PERIOD);

    ADC_setPPBReferenceOffset(myADC2_BASE, ADC_PPB_NUMBER1, 2289);
    ADC_setPPBReferenceOffset(myADC1_BASE, ADC_PPB_NUMBER1, 2289);
    ADC_setPPBReferenceOffset(myADC0_BASE, ADC_PPB_NUMBER1, 2289);

    Interrupt_enableMaster();
    // Enable Global Interrupt (INTM) and realtime interrupt (DBGM)
    EINT;
    ERTM;

//    initMotorFocPara(&motorVars);
    GPIO_writePin(EN_GATE, 0);
    Delay_MS(1);
//    GPIO_writePin(EN_GATE, 1);
    GPIO_writePin(SPI_CS, 1);

    while(1)
    {


//        Delay_MS(50);

    }
}


__interrupt void INT_mySCI0_RX_ISR(void)
{

    SCI_clearOverflowStatus(mySCI0_BASE);
    SCI_clearInterruptStatus(mySCI0_BASE, SCI_INT_RXFF);
    Interrupt_clearACKGroup(INT_mySCI0_RX_INTERRUPT_ACK_GROUP);


}
__interrupt void INT_mySCI0_TX_ISR(void)
{


    Interrupt_clearACKGroup(INT_mySCI0_TX_INTERRUPT_ACK_GROUP);
}


__interrupt void INT_mySCI1_RX_ISR(void)
{
    SCI_readCharArray(mySCI1_BASE, rDataA, 5);
    SCI_resetRxFIFO(mySCI1_BASE);

    SCI_clearOverflowStatus(mySCI1_BASE);
    SCI_clearInterruptStatus(mySCI1_BASE, SCI_INT_RXFF);
    Interrupt_clearACKGroup(INT_mySCI1_RX_INTERRUPT_ACK_GROUP);
    rxCnt++;
}

__interrupt void INT_mySCI1_TX_ISR(void)
{


    Interrupt_clearACKGroup(INT_mySCI1_TX_INTERRUPT_ACK_GROUP);
}

__interrupt void INT_mySPI0_TX_ISR(void)
{

}

__interrupt void INT_mySPI0_RX_ISR(void)
{

}

void LED_init()
{
    GPIO_setPadConfig(31, GPIO_PIN_TYPE_STD);
    GPIO_setQualificationMode(31, GPIO_QUAL_SYNC);
    GPIO_setDirectionMode(31, GPIO_DIR_MODE_OUT);
    GPIO_setControllerCore(31, GPIO_CORE_CPU1);

    GPIO_setPadConfig(34, GPIO_PIN_TYPE_STD);
    GPIO_setQualificationMode(34, GPIO_QUAL_SYNC);
    GPIO_setDirectionMode(34, GPIO_DIR_MODE_OUT);
    GPIO_setControllerCore(34, GPIO_CORE_CPU2);

    GPIO_setPadConfig(14, GPIO_PIN_TYPE_STD);
    GPIO_setQualificationMode(14, GPIO_QUAL_SYNC);
    GPIO_setDirectionMode(14, GPIO_DIR_MODE_OUT);
    GPIO_setControllerCore(14, GPIO_CORE_CPU2);
}

//
// End of File
//
